package com.txw.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.txw.commonutils.R;
import com.txw.serviceedu.entity.EduTeacher;
import com.txw.serviceedu.entity.vo.TeacherQuery;
import com.txw.serviceedu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author TanXiongWei
 * @since 2021-11-22
 */
@RestController
@RequestMapping("/serviceedu/edu-teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;


    /**
     * 查询讲师表所有数据
     *
     * @return
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {

        return R.ok().data("items", teacherService.list(null));
    }


    /**
     * -
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation(value = "讲师逻辑删除")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        if (teacherService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 分页讲师列表
     *
     * @param current 当前页
     * @param limit   每页记录条数
     * @return
     */
    @GetMapping("/pageTeacher/{current}/{limit}")
    @ApiOperation(value = "分页讲师列表")
    public R pageList(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current
            , @ApiParam(name = "", value = "每页记录数", required = true) @PathVariable Long limit) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        // 调用方法实现分页
        //调用方法的时候。底层封装，把分页所有数据封装到teacherPage对象里面
        teacherService.page(teacherPage, null);
        // 返回总记录数跟list数据集合
        return R.ok().data("total", teacherPage.getTotal()).data("rows", teacherPage.getRecords());
    }

    /**
     * 多条件组合讲师分页查询
     *
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     * @RequestBody(required = false) 使用json传递数据 把json数据封装到对应的对象里 提交方式需要改成Psot提交方式
     * required表示条件值可以为空
     */
    @ApiOperation(value = "多条件组合讲师分页查询")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current, limit);

        //条件构建
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        //多条件组合查询
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(teacherQuery.getName())) {
            //构建条件
            queryWrapper.like("name", teacherQuery.getName());//模糊查询
        }
        if (!StringUtils.isEmpty(teacherQuery.getLevel())) {
            queryWrapper.eq("level", teacherQuery.getLevel());//级别相等
        }
        if (!StringUtils.isEmpty(teacherQuery.getBegin())) {
            queryWrapper.ge("gmt_create", teacherQuery.getBegin());//大于等于开始时间
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())) {
            queryWrapper.le("gmt_modified", teacherQuery.getEnd());//小于 等于结束时间
        }
        //调用方法实现条件分页查询
        teacherService.page(teacherPage, queryWrapper);
        return R.ok().data("total", teacherPage.getTotal()).data("rows", teacherPage.getRecords());
    }

    /**
     * 添加讲师
     *
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        if (teacherService.save(eduTeacher)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 根据讲师ID查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        return R.ok().data("teacher", teacherService.getById(id));
    }

    /**
     * 讲师修改功能
     *
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "讲师修改功能")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        if (teacherService.updateById(eduTeacher)) {
            return R.ok();
        }
        return R.error();
    }
}

